<?php

include_once(APP_URL.'protected/myUtil/CheckUtil.php');
class MoneyController extends Controller {
    
    public function actionPostBcardbindMsg() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postbcardbindmsg
    
        // param check
        $userId = CheckUtil::checkPostParam(111601, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111601, 'session', false);
        $bcardPhone = CheckUtil::checkPostParam(111601, 'cardholderPhone', false);
        $bcardNum = CheckUtil::checkPostParam(111601, 'accountNum', false);
    
        // user identify
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111601, 'user not exsit !');
            exit;
        }
        
        // third part verify completed !
        // ...
    
        // send verifycode msg
        if (time() - $result->user_bcard_time <= SEND_VERIFYCODE_INTERVAL) {// 60s
            echo EchoUtil::retFailed(111601, 'msg interval time less than 60s !');
            exit;
        }
        
        $userBcardVericode = rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9);
        include_once(APP_URL.'protected/myUtil/HelperUtil.php');
        if (!HelperUtil::sendSms($bcardPhone, $userBcardVericode)) {
            echo EchoUtil::retFailed(111601, 'send msg error !');
            exit;
        }
    
        // update record
        $token = md5($bcardPhone.microtime());// generate token
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$result->user_id)
        );// fill condition
        $contentArr = array(
            'user_bcard_tk' => $token,
            'user_bcard_vc' => $userBcardVericode,
            'user_bcard_time' => time()
        );// fill content
        if (!$result->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(111602, 'post bcardbind msg failed !');
            exit;
        };

        // return success
        echo EchoUtil::msgSuccess(1, array(
            'token' => $token
        ));
    
    }
    
    public function actionPostBcardbind() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postbcardbind
    
        // check param
        $userId = CheckUtil::checkPostParam(111701, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111701, 'session', false);
        $bcardPhone = CheckUtil::checkPostParam(111701, 'cardholderPhone', false);
        $bcardNum = CheckUtil::checkPostParam(111701, 'accountNum', false);
        $token = CheckUtil::checkPostParam(111701, 'token', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111701, 'user not exsit !');
            exit;
        }
        
        // token error
        if ($token != $result->user_bcard_tk) {
            echo EchoUtil::retFailed(111703, 'token error !');
            exit;
        }
    
        $bcardType = 1;
        $bcardName = '中国邮政银行';
        // add record
        $TblBcard = new TblBcard();

        $contentArr = array(
            'bcard_uid'=>$userId,
            'bcard_type'=>$bcardType,
            'bcard_name'=>$bcardName,
            'bcard_phone'=>$bcardPhone,
            'bcard_num'=>$bcardNum,
            'bcard_def'=>2
        );// fill content
        if(!$TblBcard->addRecord('bcard', $contentArr)) {
            echo EchoUtil::retFailed(111702, 'post bcard bind failed !');
            exit;
        }

        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionPostBcardubindToken() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postbcardubindtoken
    
        // check param
        $userId = CheckUtil::checkPostParam(111801, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111801, 'session', false);
        $userPaypwd = CheckUtil::checkPostParam(111801, 'payPassword', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111801, 'user not exsit !');
            exit;
        }
    
        if ($userPaypwd != $result->user_paypwd) {
            echo EchoUtil::retFailed(111802, 'pay password error !');
            exit;
        }
    
        // update record
        $token = md5($result->user_phone.microtime());// generate token
        $conditionArr = array(
            'condition' => 'user_id=:userId',
            'params' => array('userId'=>$userId)
        );// fill condition
        $contentArr = array(
            'user_bcard_tk' => $token,
            'user_bcard_time' => time()
        );// fill content
        if (!$result->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(111802, 'bcard ubind verify failed !');
            exit;
        };

        // return success
        echo EchoUtil::msgSuccess(1, array(
            'token' => $token
        ));
    
    }
    
    public function actionPostBcardubind() {
        // http://112.74.203.16/wifishare_server/index.php?r=user/postbcardubind
    
        // check param
        $userId = CheckUtil::checkPostParam(111901, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111901, 'session', false);
        $bcardNum = CheckUtil::checkPostParam(111901, 'accountNum', false);
        $token = CheckUtil::checkPostParam(111901, 'token', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111901, 'user not exsit !');
            exit;
        }
    
        // token error
        if ($token != $result->user_bcard_tk) {
            echo EchoUtil::retFailed(111901, 'token error !');
            exit;
        }
    
        // delete record
        $TblBcard = new TblBcard();
        $conditionArr = array(
            'condition' => 'bcard_num=:bcardNum AND bcard_uid=:bcardUid',
            'params' => array('bcardNum'=>$bcardNum, 'bcardUid'=>$userId));// fill condition
        if (!$TblBcard->deleteRecord($conditionArr)) {
            echo EchoUtil::retFailed(111902, 'bcard unbind failed !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionPostBcardDef() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postbcarddef
    
        // check param
        $userId = CheckUtil::checkPostParam(112001, 'userId', false);
        $userSession = CheckUtil::checkPostParam(112001, 'session', false);
        $bcardNum = CheckUtil::checkPostParam(112001, 'accountNum', false);

        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(112001, 'user not exsit !');
            exit;
        }
        
        // update record
        $TblBcard = new TblBcard();
        $conditionArr = array(
            'condition' => 'bcard_num=:bcardNum AND bcard_uid=:bcardUid',
            'params' => array('bcardNum'=>$bcardNum, 'bcardUid'=>$userId)
        );// fill condition
        $contentArr = array(
            'bcard_def'=>1
        );// fill content
        if (!$TblBcard->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(112002, 'set default bcard failed !');
            exit;
        }
        
        $conditionArr = array(
            'condition' => 'bcard_def=:bcardDef AND bcard_uid=:bcardUid',
            'params' => array('bcardDef'=>1, 'bcardUid'=>$userId)
        );// fill condition
        $contentArr = array(
            'bcard_def'=>2
        );// fill content
        if (!$TblBcard->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(112002, 'set default bcard failed !');
            exit;
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    

    public function actionPostPay() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postpay
    
        // check param
        $userId = CheckUtil::checkPostParam(111301, 'userId', false);
        $userSession = CheckUtil::checkPostParam(111301, 'session', false);
        $pkgType = CheckUtil::checkPostParam(111301, 'packageType', false);
        $count = CheckUtil::checkPostParam(111301, 'count', false);
        $payMoney = CheckUtil::checkPostParam(111301, 'totalPrice', false);
        
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(111301, 'user not exsit !');
            exit;
        }
    
        // third part pay completed !
        //         ...
    
        $payYear = date('Y');
        $payMonth = date('n');
        $payTime = time();
        $TblPay = new TblPay();
        
        for ($i = 0; $i <= $count; $i++) {
            $conditionArr = array(
                'select' => array('*'),
                'condition' => 'pay_month=:payMonth AND pay_uid=:payUid',
                'params' => array('payMonth'=>($payMonth + $i), 'payUid'=>$userId)
            );// fill condition
            $result = $TblPay->selectRecord($conditionArr);
            if (!$result) {// no pay of the month
                $contentArr = array(
                    'pay_uid' => $userId,
                    'pay_money' => $payMoney,
                    'pay_pkgtype' => $pkgType,
                    'pay_month' => ($payMonth + $i),
                    'pay_year' => $payYear,
                    'pay_time' => $payTime
                );// fill content
                if (!$TblPay->addRecord('pay', $contentArr)) {
                    echo EchoUtil::retFailed(111301, 'post pay failed !');
                    exit;
                }
            }
        }
    
        // return success
        echo EchoUtil::msgSuccess(1, (object)array());
        
    }
    
    public function actionPostOut() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/postout
    
        // param check
        $userId = CheckUtil::checkPostParam(112101, 'userId', false);
        $userSession = CheckUtil::checkPostParam(112101, 'session', false);
        $bcardNum = CheckUtil::checkPostParam(112101, 'accountNum', false);
        $outMoney = CheckUtil::checkPostParam(112101, 'outMoney', false);
        $payPassword = CheckUtil::checkPostParam(112101, 'payPassword', false);
    
        // user identify check
        if (!$this->userIdentifyCheck($userId, $userSession)) {
            echo EchoUtil::retFailed(112101, 'user account invalid !');
            exit;
        }
    
        $TblUserObj = new TblUser();
        $result = $TblUserObj->selectSingleRecord('*', 'user_id', $userId);
        if ($payPassword != $result->user_paypwd) {
            echo EchoUtil::retFailed(112103, 'pay password error !');
            exit;
        }
    
        // third interface completed
        // ...
        $remainMoney = $result->user_remain_money - $outMoney;
        if ($remainMoney < 0 ) {
            $remainMoney = 0;
        }
        $array = array('user_remain_money' => $remainMoney);
        if (!$TblUserObj->updateRecord2('user_id', $result->user_id, $array)) {
            echo EchoUtil::retFailed(112102, 'out password failed !');
            exit;
        }
    
        echo EchoUtil::msgSuccess(1, (object)array());
    
    }
    
    public function actionGetBcard() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/getbcard
        
        // check param
        $userId = CheckUtil::checkGetParam(101101, 'userId', false);
        $userSession = CheckUtil::checkGetParam(101101, 'session', false);
        
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(101101, 'user not exsit !');
            exit;
        }
        
        // bcard not exsit
        $conditionArr = array(
            'select' => array('*'),
            'order' => 'bcard_id ASC',
            'condition' => 'bcard_uid=:bcardUid',
            'params' => array('bcardUid'=>$userId)
        );// fill condition
        $TblBcard = new TblBcard();
        $result = $TblBcard->selectRecordAll($conditionArr);
        if (!$result) {// bcard not exsit
            echo EchoUtil::retFailed(101102, 'get bcard list failed !');
            exit;
        }
    
        // select success
        foreach ($result as $key=>$value) {
            $dataArray[] = array(
                'bankTpye' => $value->bcard_type,
                'bankName' => $value->bcard_name,
                'isDefault' => $value->bcard_def,
                'accountNum' => $value->bcard_num
            );
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
    
    }

    public function actionGetPkg() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/getpkg
    
        // check param
        $userId = CheckUtil::checkGetParam(100401, 'userId', false);
        $userSession = CheckUtil::checkGetParam(100401, 'session', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(100401, 'user not exsit !');
            exit;
        }
    
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'pkg_uid=:pkgUid',
            'params' => array('pkgUid'=>$userId)
        );// fill condition
        $TblPkg = new TblPkg();
        $result = $TblPkg->selectRecord($conditionArr);
        if (!$result) {
            echo EchoUtil::retFailed(100401, 'user has not pkg !');
            exit;
        }
        
        $pkgRflow = is_null($result->pkg_rflow) ? "" : $result->pkg_rflow;
        $pkgType = is_null($result->pkg_type) ? "" : $result->pkg_type;
        $pkgDline = is_null($result->pkg_dline) ? "" : $result->pkg_dline;
    
        // return success
        echo EchoUtil::msgSuccess(1, array(
            'packageType' => $pkgType,
            'remainingFlows' => $pkgRflow,
            'deadline' => $pkgDline
        ));
    
    }

    public function actionGetPay() {
        // http://112.74.203.16/wifishare_server/index.php?r=money/getpay
    
        // check param
        $userId = CheckUtil::checkGetParam(100901, 'userId', false);
        $userSession = CheckUtil::checkGetParam(100901, 'session', false);
    
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(100901, 'user not exsit !');
            exit;
        }
    
        // user has not pay
        $conditionArr = array(
            'select' => array('*'),
            'order' => 'pay_id ASC',
            'condition' => 'pay_uid=:payUid',
            'params' => array('payUid'=>$userId)
        );// fill condition
        $TblPay = new TblPay();
        $result = $TblPay->selectRecordAll($conditionArr);
        if (!$result) {// user has not pay
            echo EchoUtil::retFailed(100902, 'get pay failed !');
            exit;
        }
    
        // select success
        foreach ($result as $key=>$value) {
            $payPkgtype = is_null($value->pay_pkgtype) ? "" : $value->pay_pkgtype;
            $dataArray[] = array(
                'packageType' => $payPkgtype,
                'paidMoney' => $value->pay_money,
                'paidTime' => $value->pay_time
            );
        }
        
        // return success
        echo EchoUtil::msgSuccess(1, array('list'=>$dataArray));
    
    }
	
}
